
import org.springframework.web.servlet.ModelAndView
import org.authsum.stitches.external.*
import org.authsum.stitches.services.*
import org.authsum.stitches.*


class ContentVersionController {

	def stitchesService
	def index = {
		//can't just go to the index, we need a contentversionid
		//let's set a message and redirect
		flash.message = "Please select a contentversion first"
		chain(controller:'contentDef',action:index)
	}
	

	
	def editContentVersion = {  

		//get the id and get the contentversion
			
		def contentVersionRequest = new ContentVersionRequest()
		
		
		
		//need to populate the content
		bindData(contentVersionRequest, params)
		
		//contentVersionRequest.contentVersionId = params['contentVersionId'].toLong()
		def contentVersionResponse =stitchesService.findContentVersionById(contentVersionRequest)
		
		def contentVersionHolder = contentVersionResponse.contentVersionHolders.contentVersionHolder.get(0)
		
		
		return new ModelAndView("/contentVersion/contentVersionForm",  [contentVersionHolder:contentVersionHolder])
		
		
		
	}
	
	def createContentVersion = {
			
		def contentVersionRequest = new ContentVersionRequest()
		def contentVersionHolder = new ContentVersionHolder()
		contentVersionRequest.contentVersionHolder = contentVersionHolder
		contentVersionRequest.contentVersionHolder.contentId = params['id'].toLong()
		return new ModelAndView("/contentVersion/contentVersionForm",  [contentVersionHolder:contentVersionHolder])
		
		
	}
	
	def saveContentVersion = {
		
		
			

		def contentVersionRequest = new ContentVersionRequest()
		def contentVersionHolder = new ContentVersionHolder()
		
		
		bindData(contentVersionHolder, params)
		
		contentVersionRequest.contentVersionHolder = contentVersionHolder
		
		def contentVersionResponse =stitchesService.saveContentVersion(contentVersionRequest)	 
		
			contentVersionResponse.messages?.string.each {
			println it
			flash.message += it
		}
		
		
		assert contentVersionResponse.responseCode == '200'

		
		contentVersionHolder =  contentVersionResponse.contentVersionHolders.contentVersionHolder.get(0)
		
		return new ModelAndView("/contentVersion/contentVersionForm",  [contentVersionHolder:contentVersionHolder])
		
		
	}
}
